package com.vitas.coin.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.vitas.coin.data.bean.MusicType;

import java.util.ArrayList;
import java.util.List;

public class TypeDao extends MyBaseDao {

    String TABLE_NAME = "type";

    @Override
    protected String getTableName() {
        return TABLE_NAME;
    }

    public boolean delete(MusicType bean) {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            int result = db.delete(getTableName(), "id=?", new String[]{String.valueOf(bean.dbId)});
            db.execSQL("DROP TABLE IF EXISTS " + bean.id);
            return result == 1;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
        return false;
    }

    public void delete(MusicType bean, DBListener listener) {
        doInBackground(new Runnable() {

            @Override
            public void run() {
                boolean bo = delete(bean);
                if (listener != null) {
                    listener.onResult(bo, "", bean);
                }
            }
        });
    }

    public MusicType insert(MusicType bean) {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            if (db == null) {
                return null;
            }
            ContentValues values = new ContentValues();
            values.put("uid", bean.id);
            values.put("name", bean.name);
            values.put("time", System.currentTimeMillis());
            bean.dbId = db.insert(TABLE_NAME, null, values);
            db.execSQL(String.format(DatabaseHelper.CREATE_LIST, bean.id));
            return bean;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
        return null;
    }

    public void insert(final MusicType bean, DBListener listener) {
        doInBackground(new Runnable() {

            @Override
            public void run() {
                MusicType tmp = insert(bean);
                if (tmp == null) {
                    listener.onResult(false, "", bean);
                } else {
                    listener.onResult(true, "", tmp);
                }
            }
        });
    }

    public boolean update(MusicType bean) {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            if (db == null) {
                return false;
            }
            ContentValues values = new ContentValues();
            values.put("uid", bean.id);
            values.put("name", bean.name);
            values.put("time", System.currentTimeMillis());
            db.update(TABLE_NAME, values, "id=?", new String[]{bean.dbId + ""});
            return true;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
        return false;
    }

    public void update(final MusicType bean, DBListener listener) {
        doInBackground(new Runnable() {

            @Override
            public void run() {
                if (update(bean)) {
                    if (listener != null) {
                        listener.onResult(true, "", bean);
                    }
                } else if (listener != null) {
                    listener.onResult(false, "", bean);
                }
            }
        });
    }

    public ArrayList<MusicType> getAll() {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = getDataBase();
            if (db == null) {
                return new ArrayList<>();
            }
            cursor = db.rawQuery("select * from " + TABLE_NAME + " order by time desc", null);
            if (cursor == null || cursor.getCount() <= 0) {
                return new ArrayList<>();
            }
            ArrayList<MusicType> list = new ArrayList<>();
            while (cursor.moveToNext()) {
                MusicType item = new MusicType();
                item.name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                item.id = cursor.getString(cursor.getColumnIndexOrThrow("uid"));
                item.dbId = cursor.getLong(cursor.getColumnIndexOrThrow("id"));
                list.add(item);
            }
            return list;
        } catch (Throwable e) {
            return new ArrayList();
        } finally {
            closeCursor(cursor);
            if (db != null) {
                closeDataBase();
            }
        }
    }

    public void getAll(DBListener listener) {
        doInBackground(new Runnable() {
            @Override
            public void run() {
                List<MusicType> list = getAll();
                if (listener != null) {
                    listener.onResult(true, null, list);
                }
            }
        });
    }
}
